#!/bin/bash

usage="Usage: `basename $0` (install | start | stop | restart | show | config | clear)"

home=`env | grep ^HOME= | cut -c 6-`
conf_filename=.sssconfig
conf_file=${home}/${conf_filename}
this_file=$(cd "$(dirname "$0")";pwd)/$0
bin_file=/usr/local/bin/$0

server=
server_port=
password=
cipher=
local_address=127.0.0.1
local_port=1080
timeout=300

if [ ! -f "$conf_file" ];then
    touch $conf_file
fi

function json_get() {
    cat $conf_file | grep '"'$1'"' | sed 's/ *"'$1'" *: *"*\([^,"]*\)"*,*/\1/g' 
}

function read_args() {
    server=`json_get server`
    server_port=`json_get server_port`
    password=`json_get password`
    cipher=`json_get method`
    local_address=`json_get local_address`
    local_port=`json_get local_port`
    timeout=`json_get timeout`
}

function show() {
    read_args

    echo -e "\n=== Shadowsocks configuration ==============================\n"
    echo "  Server        : $server"
    echo "  Server port   : $server_port"
    echo "  Method        : $cipher"
    echo "  Local address : $local_address"
    echo "  Local port    : $local_port"
    echo "  Timeout       : $timeout"
    echo -e "\n============================================================"
}

function config() {
    server=`json_get server`
    server_port=`json_get server_port`
    cipher=`json_get method`
    if [ -z "$server" ]; then
        server=*
    fi
    if [ -z "$server_port" ]; then
        server_port=8388
    fi
    if [ -z "$cipher" ]; then
        cipher=aes-256-cfb
    fi

    echo -e "\n=== Config Shadowsocks =====================================\n"

    line_max=0

    server_line="Specify server address [$server]"
    line_len=${#server_line}
    if [ $line_len -gt $line_max ];then
        line_max=$line_len
    fi

    password_line="Specify password [*]"
    line_len=${#password_line}
    if [ $line_len -gt $line_max ];then
        line_max=$line_len
    fi

    server_port_line="Specify server port [$server_port]"
    line_len=${#server_port_line}
    if [ $line_len -gt $line_max ];then
        line_max=$line_len
    fi

    cipher_line="Specify cipher method [$cipher]"
    line_len=${#cipher_line}
    if [ $line_len -gt $line_max ];then
        line_max=$line_len
    fi

    printf "%-*s : " $line_max "$server_line"
    read server
    printf "%-*s : " $line_max "$password_line"
    read password
    printf "%-*s : " $line_max "$server_port_line"
    read server_port
    printf "%-*s : " $line_max "$cipher_line"
    read cipher

    if [ -z "$server" ]; then
        server=`json_get server`
    fi
    if [ -z "$password" ]; then
        password=`json_get password`
    fi
    if [ -z "$server_port" ]; then
        server_port=`json_get server_port`
        if [ -z "$server_port" ]; then
            server_port=8388
        fi
    fi
    if [ -z "$cipher" ]; then
        cipher=`json_get method`
        if [ -z "$cipher" ]; then
            cipher=aes-256-cfb
        fi
    fi

    echo -e '{
    "server": "'$server'",
    "server_port": '$server_port',
    "password": "'$password'",
    "method": "'$cipher'",
    "local_address": "'$local_address'",
    "local_port": '$local_port',
    "timeout": '$timeout"\n}" > "$conf_file"

    show
}

function install() {
    apt-get install python -y
    easy_install pip
    pip install shadowsocks

    cp $this_file $bin_file
    chmod +x $bin_file

    echo -e 'Shadowsocks installed successfully\n'

    config
}

function start() {
    nohup sslocal -c $conf_file > /dev/null 2>&1 &

    echo 'Shadowsocks started'
}

function stop() {
    pid=`ps ax | grep sslocal | grep $conf_filename | awk '{print $1}'`
    if [ ! -z "$pid" ];then
        kill -9 $pid
    fi

    echo 'Shadowsocks stopped'
}

function restart() {
    stop
    start
}

function clear() {
    rm -f $conf_file

    echo 'Shadowsocks files cleared'
}

case $1 in
    (install)
        install
        ;;
    (start)
        start
        ;;
    (stop)
        stop
        ;;
    (restart)
        restart
        ;;
    (show)
        show
        ;;
    (config)
        config
        ;;
    (clear)
        clear
        ;;
    (*)
        echo "$usage"
        ;;
esac
